package com.gzbugu.bugusys.action;

import java.io.File;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.ServletContext;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.util.ServletContextAware;
import org.springframework.stereotype.Controller;

import com.gzbugu.bugusys.bean.PageModel;
import com.gzbugu.bugusys.service.ITrafficContractService;
import com.gzbugu.bugusys.vo.TrafficContract;
import com.opensymphony.xwork2.ActionSupport;

/**
 * 业务合同增删改查Action
 * 
 * @author gauze
 * 
 */
@Controller
public class TrafficContractAction extends ActionSupport implements
		ServletContextAware {
	/**
	 * 
	 */
	private static final long serialVersionUID = -5116524170418748283L;
	private final Log log = LogFactory.getLog(TrafficContractAction.class);
	private TrafficContract contract;// 合同
	private PageModel page;// 分页
	private String uploadContentType;// 文件类型
	private String uploadFileName;// 上传的广告名
	private File upload;// 上传的文件
	private Integer contractId;// 合同id
	@SuppressWarnings("unchecked")
	private List contracts;// 查找到的contract
	private ServletContext context;

	@Resource(name = "trafficContractService")
	ITrafficContractService contractService;

	public String addTrafficContract() {
		log.info("添加业务合同");
		File saveFile = contractService.saveUploadfile(uploadFileName, upload);
		if (saveFile.exists()) {
			this.contract.setUrl(saveFile.getAbsolutePath());
		}
		contractService.addContract(getContract());// 保存
		return SUCCESS;
	}

	public void validateAddTrafficContract() {
		if (upload == null) {
			this.addActionError("必须上传合同扫描副本");
		}
		if (upload != null
				&& !uploadContentType.toLowerCase().matches("image/.*")) {
			this.addActionError("只能上传图片格式文件");
		}
		if (this.contract == null)
			this.addActionError("请填写信息");
	}

	public String deleteTrafficContract() {
		if (this.contractId != null) {
			contractService.deleteContract(contractId);// 删除
		} else {
			return ERROR;
		}
		return SUCCESS;
	}

	public String alterTrafficContract() {
		if (contractId == null)
			return ERROR;
		File saveFile = contractService.saveUploadfile(uploadFileName, upload);
		if (saveFile != null && saveFile.exists()) {
			this.contract.setUrl(saveFile.getAbsolutePath());
		}
		contractService.alterContract(contractId, contract);// 修改
		return SUCCESS;
	}
	
	public void validateAlterTrafficContract(){
		if (upload != null
				&& !uploadContentType.toLowerCase().matches("image/.*")) {
			this.addActionError("只能上传图片格式文件");
		}
		if (this.contract == null)
			this.addActionError("请填写信息");
	}

	public String findTrafficContract() {
		if (page == null)
			this.page = new PageModel();
		page.setLineCount(contractService.countContract());// 合同总数(总记录数)
		this.contracts = contractService.findContract(page);// 查找
		return SUCCESS;
	}

	public void setContract(TrafficContract contract) {
		this.contract = contract;
	}

	public TrafficContract getContract() {
		return contract;
	}

	public void setPage(PageModel page) {
		this.page = page;
	}

	public PageModel getPage() {
		return page;
	}

	public void setUploadContentType(String uploadContentType) {
		this.uploadContentType = uploadContentType;
	}

	public String getUploadContentType() {
		return uploadContentType;
	}

	public void setUploadFileName(String uploadFileName) {
		this.uploadFileName = uploadFileName;
	}

	public String getUploadFileName() {
		return uploadFileName;
	}

	public void setUpload(File upload) {
		this.upload = upload;
	}

	public File getUpload() {
		return upload;
	}

	public void setContractId(Integer contractId) {
		this.contractId = contractId;
	}

	public Integer getContractId() {
		return contractId;
	}

	@SuppressWarnings("unchecked")
	public void setContracts(List contracts) {
		this.contracts = contracts;
	}

	@SuppressWarnings("unchecked")
	public List getContracts() {
		return contracts;
	}

	public void setServletContext(ServletContext arg0) {
		this.context = arg0;
	}
}
